<?php

namespace App\Http\Controllers\Admin;

use App\Models\Bankcard;
use App\Models\DiningReserve;
use App\Models\ProdOrder;
use App\Models\Recharge;
use App\Models\SysConfig;
use App\Models\Transition;
use Illuminate\Http\Request;

/**
 * @group 后台接口文档
 * @title 系统设置模块接口
 * @author chenke
 * @description 酒庄后台系统设置模块接口
 * @header name:X-Token type:string require:1 desc:登录令牌
 *  */
class Set extends Base
{
    /**
     * @title 获取系统设置信息
     * @description 后台获取系统设置信息
     * @url /admin/set/infoshow
     * @method GET
     * @module 系统设置板块
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:交易信息@
     * @data title:设置名称 key:设置关键词 transno:交易单号 content:设置内容
     */
    public function sysInfos(Request $request){
        $list = (new SysConfig())->get();
        $info = [];
        foreach ($list as $value){
            $info[$value->key] = $value->content;
        }
        return response()->json([
            'code'=>0,
            'data'=>[
                'info'=>json_encode($info),
                'rows'=>$list->toArray()
            ]
        ]);
    }

    /**
     * @title 设置系统信息
     * @description
     * @url /admin/set/infoset
     * @method POST
     * @module 系统设置板块
     * @author chenke
     * @param name:id type:int require:1 default:0 desc:充值ID other:
     * @param name:status type:int require:1 default: desc:要变成的充值状态 other:2审核成功3审核拒绝
     * @return code:错误码
     * @return message:错误提示
     * @return data:成功时为空
     */
    public function infoSet(Request $request){
        try {
            $data = $request->post();
            if(!empty($data)){
                $now = date('Y-m-d H:i:s');
                $result = true;
                foreach ($data as $key=>$v){
                    $result =  (new SysConfig())->where('key',$key)->update(['content'=>$v,'updated_at'=>$now]);
                    if($result===false){
                        break;
                    }
                }
                if($result){
                    return response()->json([
                        'code'=>0,
                        'data'=>""
                    ]);
                }else{
                    return response()->json([
                        'code'=>0,
                        'message'=>"保存设置失败"
                    ]);
                }
            }else{
                return response()->json([
                    'code'=>0,
                    'data'=>""
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$e->getMessage()
            ]);
        }
    }

    /**
     * @title 商家收款银行卡列表
     * @description 后台获取商家收款银行卡列表
     * @url /admin/set/bankshow
     * @method GET
     * @module 银行卡板块
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:交易信息@
     * @data cardowner:卡主姓名 bankname:归属银行名称 bankcard:银行卡号
     */
    public function bankList(){
        $list = (new Bankcard())->where('uid',0)->get();
        return response()->json([
            'code'=>0,
            'data'=>$list->toArray()
        ]);
    }

    /**
     * @title 商家收款银行卡添加
     * @description 后台添加商家收款银行卡
     * @url /admin/set/crebank
     * @method POST
     * @module 银行卡板块
     * @author chenke
     * @param name:cardowner type:string require:1 default: other: desc:卡主姓名
     * @param name:bankname type:string require:1 default: other: desc:归属银行名称
     * @param name:bankcard type:string require:1 default: other: desc:银行卡号
     * @return code:错误码
     * @return message:错误提示
     * @return data:交易信息@
     * @data 数据ID
     */
    public function bankCreate(Request $request){
        try{
            $data = $request->post();
            $rules = [
                'cardowner'=>'required|string',
                'bankname'=>'required|string',
                'bankcard'=>'required|string'
            ];
            $messages = [
                'cardowner.required'=> '请填写姓名',
                'cardowner.string'=> '姓名格式错误',
                'bankname.required'=> '请填写银行名称',
                'bankname.string'=> '银行名称格式错误',
                'bankcard.required'=> '请填写银行卡号',
                'bankcard.string'=> '银行卡号格式错误',
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $data['uid'] = 0;
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $id =  (new Bankcard())->insertGetId($data);
            if($id){
                return response()->json([
                    'code'=>0,
                    'data'=>$id
                ]);
            }else{
                return response()->json([
                    'code'=>1,
                    'message'=>"添加失败"
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$e->getMessage()
            ]);
        }
    }

    /**
     * @title 商家收款银行卡编辑
     * @description 后台编辑商家收款银行卡
     * @url /admin/set/edtbank
     * @method POST
     * @module 银行卡板块
     * @author chenke
     * @param name:id type:int require:1 default: other: desc:银行卡记录ID
     * @param name:cardowner type:string require:1 default: other: desc:卡主姓名
     * @param name:bankname type:string require:1 default: other: desc:归属银行名称
     * @param name:bankcard type:string require:1 default: other: desc:银行卡号
     * @return code:错误码
     * @return message:错误提示
     * @return data:交易信息@
     * @data 数据ID
     */
    public function bankEdit(Request $request){
        try{
            $data = $request->post();
            $rules = [
                'id'=>'required|integer',
                'cardowner'=>'required|string',
                'bankname'=>'required|string',
                'bankcard'=>'required|string'
            ];
            $messages = [
                'id.required'=> '请传ID',
                'id.integer'=> 'ID数据错误',
                'cardowner.required'=> '请填写姓名',
                'cardowner.string'=> '姓名格式错误',
                'bankname.required'=> '请填写银行名称',
                'bankname.string'=> '银行名称格式错误',
                'bankcard.required'=> '请填写银行卡号',
                'bankcard.string'=> '银行卡号格式错误',
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $card =  Bankcard::find($data['id']);
            $card->cardowner = $data['cardowner'];
            $card->bankname = $data['bankname'];
            $card->bankcard = $data['bankcard'];
            $res = $card->save();
            if($res){
                return response()->json([
                    'code'=>0,
                    'data'=>$data['id']
                ]);
            }else{
                return response()->json([
                    'code'=>1,
                    'message'=>"编辑失败"
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$e->getMessage()
            ]);
        }
    }

    /**
     * @title 商家收款银行卡删除
     * @description 后台删除商家收款银行卡
     * @url /admin/set/delbank
     * @method POST
     * @module 银行卡板块
     * @author chenke
     * @param name:id type:int require:1 default: other: desc:银行卡记录ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:成功时为空
     */
    public function bankDelete(Request $request){
        try{
            $data = $request->post();
            $rules = [
                'id'=>'required|integer'
            ];
            $messages = [
                'id.required'=> '请传ID',
                'id.integer'=> 'ID数据错误'
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $res = (new Bankcard())->where('id',$data['id'])->delete();
            if($res){
                return response()->json([
                    'code'=>0,
                    'data'=>$data['id']
                ]);
            }else{
                return response()->json([
                    'code'=>1,
                    'message'=>"删除失败"
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$e->getMessage()
            ]);
        }
    }

    /**
     * @title 后台首页数据
     * @description 后台首页数据
     * @url /admin/set/index
     * @method GET
     * @module 首页板块
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:数据信息@
     * @data regtoday:今日注册用户 regyesday:昨日注册用户 rechargetoday:今日用户充值额度 rechargeyesday:昨日用户充值额度 saletoday:今日销售额度 saleyesday:昨日销售额度
     */
    public function indexInfo(Request $request){
        try {
            $today = date('Y-m-d');
            $yesteday = date('Y-m-d',time()-24*3600);
            //今日注册用户
            $regToday = (new \App\Models\User())->whereDate('created_at',$today)->count();
            //昨日注册用户
            $regYesday = (new \App\Models\User())->whereDate('created_at',$yesteday)->count();
            //今日用户充值额度
            $rechargeToday = (new Recharge())->where('transtatus',2)->whereDate('created_at',$today)->sum('number');
            //昨日用户充值额度
            $rechargeYesday = (new Recharge())->where('transtatus',2)->whereDate('created_at',$yesteday)->sum('number');
            //今日销售额度
            $saleToday = (new Transition())->where('status',2)->whereIn('transtype',[1,2,3])->whereDate('updated_at',$today)->sum('number');
            //昨日销售额度
            $saleYesday = (new Transition())->where('status',2)->whereIn('transtype',[1,2,3])->whereDate('updated_at',$yesteday)->sum('number');
            $return = [
                'regtoday'=>$regToday,
                'regyesday'=>$regYesday,
                'rechargetoday'=>$rechargeToday,
                'rechargeyesday'=>$rechargeYesday,
                'saletoday'=>$saleToday,
                'saleyesday'=>$saleYesday
            ];
            return response()->json([
                'code'=>0,
                'data'=>$return
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$e->getMessage()
            ]);
        }
    }

    /**
     * @title 后台首页消费详情
     * @description 后台首页消费详情
     * @url /admin/set/cudetail
     * @method GET
     * @module 首页板块
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:数据信息@
     * @data transno:交易单号 transtype:交易类型(1为预定订单订金付款、2为预定订单结账付款、3为商品订单付款) number:消费金额 order_total:订单总金额 title:消费名称 youhui:订单优惠 status:订单状态(订餐时1待接单2待进餐3待结账4已结账;商品购买时2待发货3待收货4已完成) image:商品主图或餐桌主图
      */
    public function cumeDetail(Request $request){
        try {
            $today = date('Y-m-d');
            $trans = (new Transition())->whereDate('updated_at',$today)->where('status',2)->whereIn('transtype',[1,2,3])->get();
            $return = [];
            foreach ($trans as $key=>$tran){
                $return[$key]['transno'] = $tran->transno;
                $return[$key]['transtype'] = $tran->transtype;
                $return[$key]['uid'] = $tran->uid;
                $return[$key]['number'] = $tran->number;
                if($tran->transtype==3){ //商品订单
                    $order = (new ProdOrder())->leftJoin('products','products.id','=','prod_orders.proid')->where('prod_orders.id',$tran->orderid)
                            ->select('prod_orders.id','prod_orders.proprice','prod_orders.pronum','prod_orders.proid','prod_orders.youhui','prod_orders.status','products.proname_zn','products.image')
                            ->first();
                    $return[$key]['order_total'] = $order->proprice * $order->pronum;
                    $return[$key]['title'] = '购买商品-'.$order->proname_zn;
                }else{ //餐桌预定订单
                    $order = (new DiningReserve())->leftJoin('dinings','dinings.id','=','dining_reserves.tid')->where('dinings.id',$tran->orderid)
                            ->select('dining_reserves.id','dining_reserves.total','dining_reserves.earnest','dining_reserves.tid','dining_reserves.youhui','dining_reserves.status','dinings.title_zn','dinings.image')
                            ->first();
                    if($tran->transtype==2){
                        $return[$key]['order_total'] = $order->total;
                        $return[$key]['title'] = '餐桌-'.$order->title_zn.'结账';
                    }else{
                        $return[$key]['order_total'] = $order->total;
                        $return[$key]['title'] = '餐桌-'.$order->title_zn.'订金';
                    }
                }
                $return[$key]['youhui'] = $order->youhui;
                $return[$key]['status'] = $order->status;
                $return[$key]['image'] = $order->image;
            }
            return response()->json([
                'code'=>0,
                'data'=>$return
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$e->getMessage()
            ]);
        }
    }
}
